package com.baomidou.ant.controller;


import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.ApplyInfo;
import com.baomidou.ant.service.IApplyFamilyInfoService;
import com.baomidou.ant.service.IApplyInfoService;
import com.baomidou.ant.service.IApplyInternshipInfoService;
import com.baomidou.ant.service.IApplyTrainInfoService;
import com.baomidou.ant.utils.FormatUtils;
import com.baomidou.ant.utils.VoUtilsTool;
import com.baomidou.ant.vo.request.*;
import com.baomidou.ant.vo.response.ResponseListApplyInfoVo;
import com.baomidou.ant.vo.response.ResponseViewApplyInfoVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2022-09-20
 */
@RestController
@RequestMapping("/apply-info")
public class ApplyInfoController {
    @Autowired
    private IApplyInfoService applyInfoService;
    @Autowired
    private IApplyTrainInfoService applyTrainInfoService;
    @Autowired
    private IApplyInternshipInfoService applyInternshipInfoService;
    @Autowired
    private IApplyFamilyInfoService applyFamilyInfoService;

    @PostMapping("/addApplyInfo")
    public Result addApplyInfo(@RequestBody RequestFillingApplyInfoVo vo){
//        if(VoUtilsTool.checkObjFieldIsNull(vo)){
//            return Result.getFailure().setMsg("信息不完整,请确认.");
//        }
        if(!FormatUtils.isMobile(vo.getTelephone())){
            return Result.getFailure().setMsg("请填写正确的手机号!");
        }
        if(applyInfoService.saveApply(vo)){
            return Result.getSuccess().setMsg("提交成功");
        }else
            return Result.getFailure().setMsg("提交保存失败!");
    }

    @PostMapping("/checkFilling")
    public Result checkFilling(@RequestBody RequestCheckFillingVo vo){
        if(ObjectUtils.isEmpty(applyInfoService.queryByUser(vo.getTelephone()))){
            return Result.getSuccess();
        }else
            return Result.getFailure();
    }

    @PostMapping("/listApplyInfo")
    public Result listApplyInfo(@RequestBody RequestListApplyInfoVo vo){
        IPage<ResponseListApplyInfoVo> iPage=applyInfoService.listApplyInfo(vo);
        return Result.getSuccess().setData(iPage);
    }

    @PostMapping("/viewInfo")
    public Result viewInfo(@RequestBody RequestViewApplyInfoVo viewApplyInfoVo){
        ResponseViewApplyInfoVo vo=new ResponseViewApplyInfoVo();
        ApplyInfo applyInfo=applyInfoService.getById(viewApplyInfoVo.getId());
        if(ObjectUtils.isEmpty(applyInfo)){
            return Result.getFailure().setMsg("未查询到相关信息");
        }
        BeanUtils.copyProperties(applyInfo,vo);
        //
        List<RequestInternshipInfoVo> internshipInfoVos=applyInternshipInfoService.queryByAId(viewApplyInfoVo.getId());
        //
        List<RequestTrainInfoVo> trainInfoVos=applyTrainInfoService.queryByAId(viewApplyInfoVo.getId());
        //
        List<RequestFamilyInfoVo> familyInfoVos=applyFamilyInfoService.queryByAId(viewApplyInfoVo.getId());
        vo.setTrainInfoVos(trainInfoVos);
        vo.setInternshipInfoVos(internshipInfoVos);
        vo.setFamilyInfoVos(familyInfoVos);
        return Result.getSuccess().setData(vo);
    }



}
